﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MyMaster.Master" AutoEventWireup="true"
    CodeBehind="SafeConfig.aspx.cs" Inherits="FashionMural.Admin.SafeConfig" %>

<asp:Content ID="Content1" ContentPlaceHolderID="LeftMenu" runat="server">
    <script type="text/javascript">
        var showflag = true;
        function showHideUl() {
            $("#safemanage").click(
                function () {
                    if (showflag) {
                        $("#safeconfigul").hide();
                        showflag = false;
                    } else {
                        $("#safeconfigul").show();
                        showflag = true;
                    }
                }
            );
        }

        function SafeAShow(flag) {
            switch (flag) {
                case 0:
                    {
                        $("#showcurrent")[0].innerText = "我的资料";
                        var date = new Date();
                        $.ajax({
                            type: "GET",
                            async: false,
                            url: "/jsserver/admin/info.aspx",
                            data: { fresh: date.getMilliseconds() },
                            dataType: "json",
                            success: function (data) {
                                $("#username")[0].innerText = data.AdminName;
                                $("#displayname").val(data.DisplayName);
                            },
                            error: function () {

                            }
                        }
                       );
                        showorhideTable("myinfo");
                    } break;
                case 1:
                    {
                        $("#showcurrent")[0].innerText = "管理员配置"
                        var date = new Date();
                        $.ajax({
                            type: "GET",
                            async: false,
                            url: "/jsserver/admin/all.aspx",
                            data: { fresh: date.getMilliseconds() },
                            dataType: "json",
                            success: function (data) {
                                var trs = $("#adminconfigTr").parent().children();
                                if (trs.length > 2) {
                                    for (i = 0; i < trs.length - 2; i++) {
                                        $(trs[i + 1]).remove();
                                    }
                                }
                                for (i = 0; i < data.length; i++) {
                                    $("#adminconfigTr").after("<tr id='adminlist" + data[i].Id + "'><td>" + data[i].AdminName + "</td><td>" + data[i].DisplayName +
                                    "</td><td><label id='adminedit" + data[i].Id + "' class='btn' onclick=\"showAdminJumpDiv(" + data[i].Id + ")\">编辑</label><label id='admindelete" + data[i].Id +
                                    "' class='btn' onclick=\"deleteAdminInfo('" + data[i].Id + "')\">删除</label></td></tr>");
                                }
                            },
                            error: function () {

                            }
                        }
                       );
                        showorhideTable("adminconfig");
                    } break;
                case 2:
                    {
                        $("#showcurrent")[0].innerText = "日志信息";
                        showorhideTable("logininfo");
                        separateLoginRecords(1);
                        getloginRecordCount();
                    } break;
            }
        }

        function separateLoginRecords(num) {
            var date = new Date();
            $.ajax({
                type: "GET",
                async: false,
                url: "/jsserver/loginrecord/separate.aspx",
                data: { pageNumber: num, freshflag: date.getMilliseconds() },
                dataType: "json",
                success: function (data) {
                    var trs = $("#logininfoTr").parent().children();
                    if (trs.length > 3) {
                        for (i = 0; i < trs.length - 3; i++) {
                            $(trs[i + 1]).remove();
                        }
                    }
                    for (i = 0; i < data.length; i++) {
                        $("#logininfoTr").after("<tr id='loginRecord" + data[i].Id + "'><td><input id='recordcheck" + data[i].Id + "' type='checkbox' value='" + data[i].Id +
                        "'></td><td>" + GetAdminUserName(data[i].UserId) + "</td><td>" + data[i].LoginIp +
                        "</td><td>" + eval("new " + data[i].LoginTime.substr(1, data[i].LoginTime.length - 2)).toLocaleDateString() + "</td><td><label class='btn' onclick=\"deleteLoginRecord(" + data[i].Id + ")\">删除</label></td></tr>");
                    }
                },
                error: function () {

                }
            }
                       );
            $("#separatepage" + num).attr("style", "font-size:15px;color:Red;cursor:pointer;");
        }

        function getloginRecordCount() {
            var date = new Date();
            $.ajax({
                type: "GET",
                async: false,
                url: "/jsserver/loginrecord/count.aspx",
                data: { freshflag: date.getMilliseconds() },
                success: function (data) {
                    $("#separatelogin")[0].innerText = "";
                    var pageNum = parseInt(data / 10);
                    for (i = 1; i <= (pageNum + 1); i++) {
                        $("#separatelogin").append("<label id='separatepage" + i + "' style='cursor:pointer;text-decoration:underline' onclick=\"separateLoginRecords(" + i + ")\">" + i + "</label>");
                    }
                },
                error: function () {

                }
            }
                       );
        }

        function GetAdminUserName(id) {
            var date = new Date();
            var username = "";
            $.ajax({
                type: "GET",
                async: false,
                url: "/jsserver/admin/username.aspx",
                data: { adminid: id, fresh: date.getMilliseconds() },
                success: function (data) {
                    username = data;
                },
                error: function () {

                }
            }
                       );
            return username;
        }

        function deleteLoginRecord(id) {
            var date = new Date();
            $.ajax({
                type: "GET",
                async: false,
                url: "/jsserver/loginrecord/delete.aspx",
                data: { id: id, fresh: date.getMilliseconds() },
                success: function (data) {
                    if (data == "True") {
                        $("#loginRecord" + id).remove();
                    }
                },
                error: function () {

                }
            }
                       );
        }

        function deleteSelectedLoginRecord() {
            var checks = $("#logininfo input[type=checkbox]");
            for (i = 0; i < checks.length; i++) {
                if ($(checks[i]).attr("checked")) {
                    var id = $(checks[i]).val();
                    if (id >= 0) {
                        deleteLoginRecord(id);
                    }
                }
            }
        }

        function deleteAdminInfo(id) {
            var date = new Date();
            $.ajax({
                type: "GET",
                async: false,
                url: "/jsserver/admininfo/delete.aspx",
                data: { userid: id, fresh: date.getMilliseconds() },
                success: function (data) {
                    $("#adminlist" + id).remove();
                },
                error: function () {

                }
            }
                       );
        }

        function showorhideTable(id) {
            $("#myinfo").hide();
            $("#adminconfig").hide();
            $("#logininfo").hide();
            $("#" + id).show();
        }

        var checked = true;
        function seletedAll(tableId) {
            if (checked) {
                $("#" + tableId + " input[type=checkbox]").attr("checked", true);
                checked = false;
            } else {
                $("#" + tableId + " input[type=checkbox]").attr("checked", false);
                checked = true;
            }
        }

        function initSafeConfig() {
            showHideUl();
            SafeAShow(0);
            showorhideTable("myinfo");
            $("#jumpDivAdminInfo").hide();
            $("#surenewpassword").hide();
        }

        function updateMyDate() {
            if ($("#newpassword").val() != $("#surepassword").val()) {
                alert('密码不一致!');
            }
            $.ajax({
                type: "POST",
                async: false,
                url: "/jsserver/admininfo/update",
                data: { adminname: $("#username")[0].innerText, oldpassword: $("#oldpassword").val(), newpassword: $("#newpassword").val(), displayname: $("#displayname").val() },
                success: function (data) {

                },
                error: function () {

                }
            }
                       );
        }

        function updateMyDate1() {
            $.ajax({
                type: "POST",
                async: false,
                url: "/jsserver/admininfo/update1.aspx",
                data: { id: $("#adminid").val(), adminname: $("#adminName").val(), displayname: $("#displayna").val(), password: $("#Text2").val() },
                success: function (data) {
                    $("#jumpDivAdminInfo").hide();
                },
                error: function () {

                }
            }
                       );
        }

        function addAdminInfo() {
            var pass = $("#Text2").val();
            var surepass = $("#Text4").val();
            if (pass != surepass) {
                alert('两次输入密码不一致！');
            }
            else {
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "/jsserver/admininfo/add.aspx",
                    data: { adminname: $("#adminName").val(), displayname: $("#displayna").val(), password: $("#Text2").val() },
                    success: function (data) {
                        if (data > 0) {
                            $("#adminconfigTr").after("<tr id='adminlist" + data + "'><td>" + $("#adminName").val() + "</td><td>" + $("#displayna").val() +
                                    "</td><td><label id='adminedit" + data + "' class='btn' onclick=\"showAdminJumpDiv(" + data + ")\">编辑</label><label id='admindelete" + data +
                                    "' class='btn' onclick=\"deleteAdminInfo('" + data + "')\">删除</label></td></tr>");
                            $("#jumpDivAdminInfo").hide();
                        } else {
                            $("#jumpDivAdminInfo").hide();
                            alert('添加失败！');
                        }
                    },
                    error: function () {
                        $("#jumpDivAdminInfo").hide();
                        alert('添加失败！');
                    }
                }
                       );
            }
        }

        function showAddAdminJumpDiv() {
            $("#jumpDivAdminInfo").show();
            $("#surenewpassword").show();
            $("#Button1").unbind("click");
            $("#Button1").click(function () {
                addAdminInfo();
            });
        }

        function showAdminJumpDiv(id) {
            $("#jumpDivAdminInfo").show();
            $("#surenewpassword").hide();
            $("#Button1").unbind("click");
            $("#Button1").click(function () {
                updateMyDate1();
            });
            var date = new Date();
            $.ajax({
                type: "GET",
                async: false,
                url: "/jsserver/admin/info.aspx",
                data: { date: date.getMilliseconds() },
                dataType: "json",
                success: function (data) {
                    if (data != null) {
                        $("#adminid").val(data.Id);
                        $("#adminName").val(data.AdminName);
                        $("#displayna").val(data.DisplayName);
                        $("#Text2").val(data.Password);
                    }
                },
                error: function () {

                }
            }
                       );
        }

        $().ready(function () {
            initSafeConfig();
        });
    </script>
    <div class="leftMenuTitle" id="safemanage" style="cursor: pointer;">
        安全配置
    </div>
    <ul class="leftMenu" id="safeconfigul">
        <li><a href="#" onclick="SafeAShow(0)">我的资料</a></li>
        <li><a href="#" onclick="SafeAShow(1)">管理员配置</a></li>
        <li><a href="#" onclick="SafeAShow(2)">日志信息</a></li>
    </ul>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MidContent" runat="server">
    <div id="mainMenu">
        <ul>
            <li><a id="showcurrent" href="#"></a></li>
        </ul>
    </div>
    <div id="main">
        <table id="myinfo" class="mainTable">
            <tr>
                <td style="text-align: right">
                    登录账号:
                </td>
                <td style="text-align: left">
                    <label id="username">
                    </label>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                    显示名:
                </td>
                <td style="text-align: left">
                    <input type="text" id="displayname" />
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                    原始密码:
                </td>
                <td style="text-align: left">
                    <input type="text" id="oldpassword" />
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                    新密码:
                </td>
                <td style="text-align: left">
                    <input type="text" id="newpassword" />
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                    确认新密码:
                </td>
                <td style="text-align: left">
                    <input type="text" id="surepassword" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input class="btn" style="height: 20px; width: 80px;" id="updateadmininfobtn" type="button"
                        value="更新" onclick="updateMyDate()" />
                </td>
            </tr>
        </table>
        <table id="adminconfig" class="mainTable">
            <tr id="adminconfigTr" style="background-color: #D7D7D7;">
                <th>
                    用户名
                </th>
                <th>
                    显示名
                </th>
                <th>
                    操作
                </th>
            </tr>
            <tr>
                <td colspan="3">
                    <input type="button" class="btn" style="height: 20px; width: 80px;" id="addadminfo"
                        value="添加用户" onclick="showAddAdminJumpDiv()" />
                </td>
            </tr>
        </table>
        <table id="logininfo" class="mainTable">
            <tr id="logininfoTr" style="background-color: #D7D7D7;">
                <th>
                    选择
                </th>
                <th>
                    用户名
                </th>
                <th>
                    登录IP
                </th>
                <th>
                    登录时间
                </th>
                <th>
                    操作
                </th>
            </tr>
            <tr>
                <td colspan="5">
                    全选<input type="checkbox" value="-1" onclick="seletedAll('logininfo')" />
                    <input type="button" class="btn" style="height: 20px; width: 80px;" id="deleteSelectedbtn"
                        value="删除选中项" onclick="deleteSelectedLoginRecord()" />
                </td>
            </tr>
            <tr>
                <td id="separatelogin" colspan="5">
                </td>
            </tr>
        </table>
        <div id="jumpDivAdminInfo" class="jumpdiv-back">
            <div class="jumpdiv-forward">
                <table>
                    <tr>
                        <td style="text-align: right">
                            登录账号:
                        </td>
                        <td style="text-align: left">
                            <input type="hidden" id="adminid" />
                            <input type="text" id="adminName" />
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: right">
                            显示名:
                        </td>
                        <td style="text-align: left">
                            <input type="text" id="displayna" />
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: right">
                            密码:
                        </td>
                        <td style="text-align: left">
                            <input type="text" id="Text2" />
                        </td>
                    </tr>
                    <%--                    <tr>
                        <td style="text-align: right">
                            新密码:
                        </td>
                        <td style="text-align: left">
                            <input type="text" id="Text3" />
                        </td>
                    </tr>--%>
                    <tr id="surenewpassword">
                        <td style="text-align: right">
                            确认新密码:
                        </td>
                        <td style="text-align: left">
                            <input type="text" id="Text4" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input class="btn" style="height: 20px; width: 80px;" id="Button1" type="button"
                                value="更新" />
                            <input class="btn" style="height: 20px; width: 80px;" id="Button2" type="button"
                                value="取消" onclick="$('#jumpDivAdminInfo').hide();" />
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</asp:Content>
